// directory.h 
//	Data structures to manage a UNIX-like directory of file names.
// 
//      A directory is a table of pairs: <file name, sector #>,
//	giving the name of each file in the directory, and 
//	where to find its file header (the data structure describing
//	where to find the file's data blocks) on disk.
//
//      We assume mutual exclusion is provided by the caller.
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved.  See copyright.h for copyright notice and limitation 
// of liability and disclaimer of warranty provisions.

#include "copyright.h"

#ifndef DIRECTORY_H
#define DIRECTORY_H

#include "openfile.h"
//edit
#include "pbitmap.h"

#define FileNameMaxLen 		128	// for simplicity, we assume 
// file names are <= 9 characters long
//edit

// The following class defines a "directory entry", representing a file
// in the directory.  Each entry gives the name of the file, and where
// the file's header is to be found on disk.
//
// Internal data structures kept public so that Directory operations can
// access them directly.

class DirectoryEntry {
public:
	DirectoryEntry() {
		isDir = false;
	}
	bool isDir;
	bool inUse; // Is this directory entry in use?
	int sector; // Location on disk to find the
	//   FileHeader for this file
//	int namesector; //The sector to store the file name
	char name[FileNameMaxLen + 1];
};

// The following class defines a UNIX-like "directory".  Each entry in
// the directory describes a file, and where to find it on disk.
//
// The directory data structure can be stored in memory, or on disk.
// When it is on disk, it is stored as a regular Nachos file.
//
// The constructor initializes a directory structure in memory; the
// FetchFrom/WriteBack operations shuffle the directory information
// from/to disk. 

class Directory {
public:
	Directory(int size); // Initialize an empty directory
	Directory(int size,int self, int parent);
	// with space for "size" files
	~Directory(); // De-allocate the directory

	void FetchFrom(OpenFile *file); // Init directory contents from disk
	void WriteBack(OpenFile *file); // Write modifications to
	// directory contents back to disk

	bool AddFile(char *name, int newSector, PersistentBitmap *freeMap);
	bool AddDir(char *name, int newSector, PersistentBitmap *freeMap, int psector);

	int FindFile(char *name); // Find the sector number of the
	int FindDir(char *name); // Find the sector number of the

	bool RemoveFile(char *name, PersistentBitmap *freeMap); // Remove a file from the directory    //edit
	bool RemoveDir(char *name, PersistentBitmap *freeMap); // Remove a file from the directory    //edit

	void Rename(char *sour, char* dest);

	void setParentDir(int sector){parentSector = sector;}
	int getParentDir(){return parentSector;}

	int getSelfDir(){return selfSector;}
	void setSelfDir(int self){selfSector = self;}

//	bool EnterChildDir(Director);

	void List(); // Print the names of all the files
	//  in the directory
	void Print(); // Verbose print of the contents
	//  of the directory -- all the file
	//  names and their contents.
	void PrintSectors();
	bool isEmpty();
public:
	bool Add(char *name, int newSector, PersistentBitmap *freeMap, bool isDir, int psector); // Add a file name into the directory    //edit

	bool Remove(char *name, PersistentBitmap *freeMap, bool isDir); // Remove a file from the directory    //edit

	int Find(char *name, bool isDir); // Find the sector number of the
	// FileHeader for file: "name"
	int FindDirIndex(char *name);

	int FindFileIndex(char *name);

	int FindIndex(char *name, bool isDir); // Find the index into the directory

private:
	int tableSize; // Number of directory entries
	DirectoryEntry *table; // Table of pairs:
	// <file name, file header location>

	//  table corresponding to "name"
	int parentSector;
	int selfSector;
};

#endif // DIRECTORY_H
